package tests.delete;

import static org.junit.Assert.*;

import java.net.UnknownHostException;
import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.BridgeInterface;
import cc.test.bridge.LineInterface;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

public class LineDeleteTest01
{
	Budget budget;
	LineInterface line1;
	LineInterface line2;
	BudgetFactory factory;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
	}

	@After
	public void tearDown() throws Exception
	{
		Mongo mongo = null;
  		try
  		{
  			mongo = new Mongo();
  		} catch (UnknownHostException e)
  		{
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		} catch (MongoException e)
  		{
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  		
          DB db = mongo.getDB("cc");
          DBCollection collection = db.getCollection(budget.getRepositoryName());
          collection.remove(new BasicDBObject("entry.id", budget.getId()));
          
	}

	@Test
	public void test()
	{
		factory = new BudgetFactory();
        
	 	 try
		 {
        
            //Create a budget
	        budget = (Budget) factory.create();
            budget.setName("Test budget");
            
            budget.setDescription("This is a delete test budget.");
            
            line1 = (Line) budget.createLine();
            line2 = (Line) budget.createLine();
            
            line1.setName("Line 1 delete test");
            line2.setName("Line 2 delete test");
            
            budget.commit();
            
            budget.delete(line1);
            
            Boolean successful = budget.commit();
            
            if(!successful)
            	fail("Line failed to delete");
            
            ArrayList<BasicDBObject> bdbo = new ArrayList<BasicDBObject>();
            bdbo.add(new BasicDBObject("entry.name", "Line 2 delete test"));
			String criteria = JSON.serialize(bdbo);
			
			@SuppressWarnings("unchecked")
			ArrayList<BridgeInterface> results =  Helper.query(criteria, WorkletContext.getInstance());
			
			Boolean seenOnce = false;
			
			for(BridgeInterface bridge : results){
				if(bridge instanceof LineInterface){
					String name = ((Line) bridge).getName();
					
					
					if(name.equals("Line 1 delete test"))
					{
						fail("Line not deleted");
					}
					else if(name.equals("Line 2 delete test"))
					{
						seenOnce = true;
					}
				}
			}
			
			if(!seenOnce)
			{
				fail("Line 2 deleted when only Line 1 was supposed to be deleted");
			}
            
        }
        catch(Exception e) {
            fail("Creating Budget failed with exception: " + e);
        }
	}
}
